// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var router = require("../hash/pageRouter");
	var top = require("../top/top_main");
	var footer = require("../footer/footer_main");
	var commodityCategoryId;
	var storage = window.sessionStorage;
	var pageIndex = 1,
		pageSize = 10,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化

	var init = function() {
		if(storage.getItem("mIndex")!=null){
			pageIndex = storage.getItem("mIndex");
		}
		top.init();
		footer.init();
		router.init();
		$("#head").removeClass("none");
		$("#content").removeClass("none");
		$("#footer").removeClass("none");
		inquireCommodityCategoryPageList();
		inquireBulkstockLatestCollectionList();
//		setInterval(function() {
//			inquireBulkstockLatestCollectionList();
//			if (pageIndex >= totalPageCount) {
//				pageIndex = 1;
//			} else {
//				pageIndex++;
//			}
//		}, 20000);
		initEvent();
	};

	var initEvent = function() {
		$("#publicInquire").on("mouseenter",function(){
			$(".w1366 div").css("background-color","#FFF");
			$(".w1366  div").css("display","block");
			$(".w1366 span").css("display","inline-block");
			$(".w1366 .bulkstockPledge1").css("display","none");
			
		});
		
		$(".w1366Div").on("mouseleave",function(){
			$(".w1366 div").css("background-color","#DC382C");
			$(".w1366 span").css("display","none");
		});
		$("#publicInquire").addClass("nav_active");

		//		添加跳转
		$("#bulkstockGather").unbind("click").click(function() {
			location.href = "../publicInquire/publicInquireMain.html";
		});

		$("#bulkstockCollection").unbind("click").click(function() {
			location.href = "../publicInquire/publicInquireCollection.html";
		});

		$("#bulkstockPledge").unbind("click").click(function() {
			base.openUrl("../publicInquire/publicInquirePledge.html");
		});
	};

	//大宗商品类别列表
	var inquireCommodityCategoryPageList = function() {
		base.fnAjax({
			action: "inquireBulkstockCategoryPageList.json",
			async: false,
			nocode: true,
			oData: {
				activeState: true,
				pageSize: 1000
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					var tpl = $("#commodityCategoryListTpl").html();
					var html = juicer(tpl, data);
					$("#commodityCategoryListBody").html(html);
					$(".type").unbind("click").click(function() {
						$("#bulkstocGatherListBody").html("");
						pageIndex = 1;
						inquireBulkstockLatestCollectionList($(this).attr("commodityCategoryId"));
						$(".type").removeClass("type_active");
						$(this).addClass("type_active");
					});
				} else {}
			}
		});
	};

	//大宗商品行情表
	var inquireBulkstockLatestCollectionList = function(commodityCategoryId) {
		base.fnAjax({
			action: "inquireBulkstockLatestCollectionList.json",
			async: false,
			nocode: true,
			oData: {
				pageIndex: pageIndex,
				pageSize: pageSize,
				bulkstockCategoryId: commodityCategoryId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					var tpl = $("#bulkstocGatherListTpl").html();
					var html = juicer(tpl, data);
					$("#bulkstocGatherListBody").html(html);
					totalPageCount = data.totalPageCount;
					totalRecordCount = data.totalRecordCount;
					base.pageControl($("#changepage"),
						totalRecordCount,
						data.pageIndex,
						data.pageSize,
						function(p) {
							pageIndex = p;
							storage.setItem("mIndex", pageIndex);
							inquireBulkstockLatestCollectionList(commodityCategoryId);
						});
					riseAndFallColor();
					//					添加监测跳转
					$(".publicMonitor").unbind("click").click(function() {
						var bulkstockname = $(this).parent().parent().attr("bulkstockname");
						var specification = $(this).parent().parent().attr("specification");
						var latestgatherId = $(this).parent().parent().attr("latestgatherid");
						var bulkstockCategoryId = $(this).parents("tr").attr("bulkstockCategoryId");
						var region = $(this).parent().parent().attr("region");
						location.href = "../publicInquire/publicInquireCollectionMonitor.html?bulkstockName=" + encodeURI(encodeURI(bulkstockname)) + "&specification=" + encodeURI(encodeURI(specification)) + "&region=" + encodeURI(encodeURI(region)) + "&latestgatherId=" + latestgatherId+"&bulkstockCategoryId"+bulkstockCategoryId;
					});
				} else {}
			}
		});
	};

	//改变涨跌颜色
	var riseAndFallColor = function() {
		var riseAndFall = $(".riseAndFall");
		for (var i = 0, j = riseAndFall.length; i < j; i++) {
			if (riseAndFall[i].innerHTML.charAt(0) == "-") {
				$(riseAndFall[i]).css("color", "#33A55E");
			} else if (riseAndFall[i].innerHTML.charAt(0) == "+") {
				$(riseAndFall[i]).css("color", "#F1592A");
			} else {
			}
		}
	};

	return {
		init: init
	};
});